CPRE := @
ifeq ($(V),1)
CPRE :=
VERB := --verbose
endif

MK_GENERATED_IMGS_PATH:=generated

.PHONY:startup
startup: all

all:
	@echo "Build Solution by $(BOARD) "
	$(CPRE) touch -acfm app/src/app_sys.c
	$(CPRE) scons $(VERB) --board=$(BOARD) -j4
	@echo YoC SDK Done

	@echo [INFO] Create bin files
	$(CPRE) product image $(MK_GENERATED_IMGS_PATH)/images.zip -i $(MK_GENERATED_IMGS_PATH)/data -l -p
	$(CPRE) product image $(MK_GENERATED_IMGS_PATH)/images.zip -e $(MK_GENERATED_IMGS_PATH)/data
	$(CPRE) product image $(MK_GENERATED_IMGS_PATH)/images.zip -e $(MK_GENERATED_IMGS_PATH)/data -x

	@echo [INFO] Create wm bin files
	$(CPRE) csky-elfabiv2-objcopy -O binary yoc.elf $(MK_GENERATED_IMGS_PATH)/yoc.bin
	$(CPRE) $(MK_GENERATED_IMGS_PATH)/wm_tool -b $(MK_GENERATED_IMGS_PATH)/yoc.bin -fc 0 -it 1 -ih 80D0000 -ra 80D0400 -ua 8010000 -nh 0 -un 0
	$(CPRE) cat $(MK_GENERATED_IMGS_PATH)/TG7101C_secboot.img $(MK_GENERATED_IMGS_PATH)/yoc.img > $(MK_GENERATED_IMGS_PATH)/yoc.fls
	$(CPRE) $(MK_GENERATED_IMGS_PATH)/wm_tool -b $(MK_GENERATED_IMGS_PATH)/yoc.img -fc 1 -it 1 -ih 80D0000 -ra 80D0400 -ua 8010000 -nh 0 -un 0
	$(CPRE) mv $(MK_GENERATED_IMGS_PATH)/yoc_gz.img $(MK_GENERATED_IMGS_PATH)/yoc_ota.bin

	@echo [INFO] Create fls file
	$(CPRE) cp $(MK_GENERATED_IMGS_PATH)/data/boot $(MK_GENERATED_IMGS_PATH)/boot.bin
	$(CPRE) cp $(MK_GENERATED_IMGS_PATH)/data/prim $(MK_GENERATED_IMGS_PATH)/prim.bin
	$(CPRE) cp $(MK_GENERATED_IMGS_PATH)/data/imtb $(MK_GENERATED_IMGS_PATH)/imtb.bin
	$(CPRE) $(MK_GENERATED_IMGS_PATH)/wm_tool -b $(MK_GENERATED_IMGS_PATH)/boot.bin -fc 0 -it 512 -ih 8002000 -ra 8002400 -ua 8010000 -nh 80D0000 -un 0
	$(CPRE) $(MK_GENERATED_IMGS_PATH)/wm_tool -b $(MK_GENERATED_IMGS_PATH)/prim.bin -fc 0 -it 1 -ih 80D0000 -ra 80D0400 -ua 8010000 -nh 0 -un 0
	$(CPRE) $(MK_GENERATED_IMGS_PATH)/wm_tool -b $(MK_GENERATED_IMGS_PATH)/imtb.bin -fc 0 -it 1 -ih 20047400 -ra 81C0000 -ua 0 -nh 0 -un 0
	$(CPRE) cd $(MK_GENERATED_IMGS_PATH) && cat boot.img prim.img imtb.img > yoc_total.fls

.PHONY:flash
flash:
	$(CPRE) scons --flash=prim --board=$(BOARD) --sdk=$(SDK)

.PHONY:flashall
flashall:
	$(CPRE) scons --flash=all --board=$(BOARD) --sdk=$(SDK)

sdk:
	$(CPRE) yoc sdk
.PHONY:clean
clean:
	$(CPRE) scons -c
	$(CPRE) find . -name "*.[od]" -delete
	$(CPRE) rm yoc_sdk yoc* $(MK_GENERATED_IMGS_PATH) out -rf